package net.trombonium.steroid;

import java.util.*;

public class Survey {

	private TreeMap<Integer, Item> items;
	private int id;
	private int firstItem;
	
	public Survey(int i){
		items = new TreeMap<Integer, Item>();
		id = i;
		firstItem = -1;
	}
	
	public void addItem(Item i){
		Integer id = new Integer(i.getId());
		if(firstItem<0 || id<firstItem)
			firstItem = id;
		items.put(id, i);
	}
	
	//Accessors:
	public Item getItem(int id){
		return items.get(new Integer(id));
	}
	public Item getFirstItem(){
		return getItem(firstItem);
	}
	public int getId(){
		return id;
	}
	
	//Demo method
	public static Survey generateDemo(){
		Survey s = new Survey(1);
		
		Item i = new Item(0, "<html><body>Do you like cats?<br /><br /><input type='button' value='Yes' onclick='window.Surveyor.setResponse(\"0\")' /><br /><input type='button' value='No' onclick='window.Surveyor.setResponse(\"1\")' /></body></html>");
		i.addResponse(Response.INTEGER);
		i.setNext(1, "0");
		i.setNext(2, "1");
		s.addItem(i);
		i = new Item(1, "<html><body>What is there to like about cats?<br /><br /><input type='text' id='ans' /><input type='button' value='Next' onclick='window.Surveyor.setResponse(document.getElementById(\"ans\").value)' /><script language='javascript'>document.getElementById(\"ans\").focus();</script></body></html>");
		i.addResponse(Response.STRING);
		i.setNext(4);
		s.addItem(i);
		i = new Item(2, "<html><body>Do you like dogs?<br /><br /><input type='button' value='Yes' onclick='window.Surveyor.setResponse(\"0\")' /><br /><input type='button' value='No' onclick='window.Surveyor.setResponse(\"1\")' /></body></html>");
		i.addResponse(Response.INTEGER);
		i.setNext(4, "0");
		i.setNext(3, "1");
		s.addItem(i);
		i = new Item(3, "<html><body>Do you like anything?<br /><br /><input type='button' value='Yes' onclick='window.Surveyor.setResponse(\"0\")' /><br /><input type='button' value='No' onclick='window.Surveyor.setResponse(\"1\")' /></body></html>");
		i.addResponse(Response.INTEGER);
		i.setNext(4);
		s.addItem(i);
		i = new Item(4, "<html><body>How does this picture make you feel?<br /><br /><img src='garfield.png' /><br /><input type='text' id='ans' /><input type='button' value='Done' onclick='window.Surveyor.setResponse(document.getElementById(\"ans\").value)' /><script language='javascript'>document.getElementById(\"ans\").focus();</script></body></html>");
		i.addResponse(Response.STRING);
		i.setNext(Item.SURVEY_END);
		s.addItem(i);
		
		return s;
	}
}

